package net.arvin.mobilebanksystem.activity;

import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.fragment.BindCardFragment;
import net.arvin.mobilebanksystem.fragment.MainPageFragment;
import net.arvin.mobilebanksystem.fragment.MenuFragment;
import net.arvin.mobilebanksystem.fragment.SettingFragment;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

/*
 * not used
 */
public class MainActivity extends SlidingFragmentActivity implements
		OnClickListener {
	private Fragment mContent;
	private TextView titleTv;
	private RelativeLayout backBtn;
	private RelativeLayout menuBtn;
	private RelativeLayout addBtn;
	private RelativeLayout saveBtn;
	public static MainActivity mainActivity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initAll();
	}

	public void initAll() {
		findView();
		initSlidingMenu();
		initListener();
		mainActivity = this;
	}

	public void findView() {
		titleTv = (TextView) findViewById(R.id.titleTv);
		backBtn = (RelativeLayout) findViewById(R.id.leftBtnLayout);
		menuBtn = (RelativeLayout) findViewById(R.id.menuBtn);
		addBtn = (RelativeLayout) findViewById(R.id.addCards);
		saveBtn = (RelativeLayout) findViewById(R.id.saveBtn);
	}

	public void initSlidingMenu() {
		setBehindContentView(R.layout.frame_menu);
		getSlidingMenu().setSlidingEnabled(true);
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

		SlidingMenu sm = getSlidingMenu();
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeEnabled(false);
		sm.setBehindScrollScale(0.4f);
		sm.setFadeDegree(0.4f);

		sm.setBackgroundImage(R.drawable.air_bg);
		sm.setBehindCanvasTransformer(new SlidingMenu.CanvasTransformer() {
			@Override
			public void transformCanvas(Canvas canvas, float percentOpen) {
				float scale = (float) (percentOpen * 0.25 + 0.75);
				canvas.scale(scale, scale, -canvas.getWidth() / 2,
						canvas.getHeight() / 2);
			}
		});

		sm.setAboveCanvasTransformer(new SlidingMenu.CanvasTransformer() {
			@Override
			public void transformCanvas(Canvas canvas, float percentOpen) {
				float scale = (float) (1 - percentOpen * 0.25);
				canvas.scale(scale, scale, 0, canvas.getHeight() / 2);
			}
		});

		switchContent(new MainPageFragment(), true);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new MenuFragment(sm)).commit();
	}

	public void initListener() {
		backBtn.setOnClickListener(this);
		menuBtn.setOnClickListener(this);
		addBtn.setOnClickListener(this);
		saveBtn.setOnClickListener(this);
	}

	public void hidenBackBtn() {
		menuBtn.setVisibility(View.VISIBLE);
		backBtn.setVisibility(View.GONE);
	}

	public void showBackBtn() {
		menuBtn.setVisibility(View.GONE);
		backBtn.setVisibility(View.VISIBLE);
	}

	public void hidenAddBtn() {
		addBtn.setVisibility(View.GONE);
	}

	public void showAddBtn() {
		addBtn.setVisibility(View.VISIBLE);
	}

	public void hidenSaveBtn() {
		saveBtn.setVisibility(View.GONE);
	}

	public void showSaveBtn() {
		saveBtn.setVisibility(View.VISIBLE);
	}

	public void setTitleTv(String titleString) {
		titleTv.setText(titleString);
	}

	public void setIsCanOpenMenu(boolean isCanOpenMenu) {
		getSlidingMenu().setSlidingEnabled(isCanOpenMenu);
	}

	public void switchContent(Fragment mFragment, boolean isFromMenu) {
		mContent = mFragment;
		if (isFromMenu) {
			for (int i = 0; i < getSupportFragmentManager()
					.getBackStackEntryCount(); i++) {
				getSupportFragmentManager().popBackStack();
			}
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_frame, mContent).commit();
		} else {
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_frame, mContent).addToBackStack("")
					.commit();
		}
	}

	public void popBackStack() {
		getSupportFragmentManager().popBackStack();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtnLayout:
			backOptions();
			break;
		case R.id.addCards:
			setTitleTv("绑定银行卡");
			switchContent(new BindCardFragment(), false);
			break;
		case R.id.menuBtn:
			showMenu();
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			backOptions();
		}
		return true;
	}

	private void backOptions() {
		if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
			if (mContent instanceof SettingFragment) {
				SettingFragment.mSetting.savaUser();
			}
			popBackStack();
		} else {
			finish();
		}
	}

	/*
	 * 若使用keydown来showMenu时一定的重写keyup，因为其父类keyup中会调用showcontent方法
	 */
	@Override
	public boolean onKeyUp(final int keyCode, final KeyEvent event) {
		return true;
	}

}
